package com.hooper.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hooper.login.entity.CityInfo;
import com.hooper.login.mapper.CityInfoMapper;
import com.hooper.login.service.CityInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.hooper.login.constant.CityConstant.ROOT_ID;

/**
 * @author Mind
 * @description 针对表【city_info】的数据库操作Service实现
 * @createDate 2023-01-31 14:46:43
 */
@Service
public class CityInfoServiceImpl extends ServiceImpl<CityInfoMapper, CityInfo>
        implements CityInfoService {

    @Override
    public void addCity(String cityName) {
        if (StringUtils.isBlank(cityName)) {
            throw new RuntimeException("param empty");
        }
        CityInfo cityInfo = new CityInfo();
        cityInfo.setParentId(ROOT_ID);
        cityInfo.setName(cityName);
        cityInfo.setCreateTime(new Date());
        cityInfo.setUpdateTime(new Date());
        baseMapper.insert(cityInfo);
    }

    @Override
    public List<CityInfo> getCityList() {
        return baseMapper.selectList(new LambdaQueryWrapper<CityInfo>() {
        }.eq(CityInfo::getParentId, ROOT_ID));
    }

    @Override
    public void addAddress(String cityName, String addressName) {
        if (StringUtils.isAnyBlank(cityName, addressName)) {
            throw new RuntimeException("PARAM EMPTY!");
        }
        LambdaQueryWrapper<CityInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CityInfo::getName, cityName);
        CityInfo city = baseMapper.selectOne(wrapper);
        CityInfo cityInfo = new CityInfo();
        cityInfo.setName(addressName);
        cityInfo.setParentId(city.getId());
        cityInfo.setCreateTime(new Date());
        cityInfo.setUpdateTime(new Date());
        baseMapper.insert(cityInfo);
    }
}




